import { getPlatform } from "../platform";
import { Mission } from "./Mission";

export class MissionManager {


    missions: Mission[] = [];

    getMissionById(id: number) {
        return this.missions.find(mission => mission.id === id);
    }

    updateAcceptable() {
        for (let mission of this.missions) {
            mission.updateAcceptable();
        }
    }

    loadConfig(callback: Function) {
        getPlatform().loadText('assets/json/missions.json', (response) => {
            const json = JSON.parse(response);
            for (let missionData of json.data) {
                const mission = new Mission();

                mission.id = missionData.id;
                mission.name = missionData.name;
                mission.toItem = missionData.toItem;
                mission.conditionType = missionData.conditionType;
                mission.conditionTotal = missionData.conditionTotal;
                mission.conditionCurrent = 0;
                mission.conditionItemIds = missionData.conditionItemIds;
                mission.preMission = missionData.preMission;

                this.missions.push(mission);
            }
            this.updateAcceptable();


            console.log(this.missions)
            callback();
        })
    }

}


export const missionManager = new MissionManager();

window['missionManager'] = missionManager;